package risoe.syslab.fhnew.hw.enocean;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public abstract class EnoceanMessage {

  private static long globalMessageNumber;
  protected char hseq;
  protected char org;
  private long messageNumber;
  protected char[] messageData;
  protected int physicalAddress;
  protected char messageStatus;
  protected int channel = 1; // This is the default value

  protected EnoceanMessage(int address) {
    messageData=new char[4];
    physicalAddress=address;
  }

  protected EnoceanMessage(char[] msgBuf) {
    messageData=new char[4];
    messageNumber=globalMessageNumber++;
    for (int i=2;i<6;i++) {
      messageData[5-i]=msgBuf[i]; //reverse byte order
    }
    messageStatus=msgBuf[10];
    
    int phys=(((int)msgBuf[6])<<24)+
    (((int)msgBuf[7])<<16)+
    (((int)msgBuf[8])<<8)+
    ((int)msgBuf[9]);
    physicalAddress = phys;
  }

  public long getMessageNumber() {
    return messageNumber;
  }

  public char[] getRawData() {
    return messageData;
  }

  public char[] getDataPacket() {
    char rv[]=new char[14];
    rv[0]=0xa5;
    rv[1]=0x5a;
    rv[2]=hseq;
    rv[3]=org;
    rv[4]=messageData[3];
    rv[5]=messageData[2];
    rv[6]=messageData[1];
    rv[7]=messageData[0];
    rv[8]=(char)((physicalAddress>>24)&0xff);
    rv[9]=(char)((physicalAddress>>16)&0xff);
    rv[10]=(char)((physicalAddress>>8)&0xff);
    rv[11]=(char)(physicalAddress&0xff);
    rv[12]=messageStatus;
    rv[13]=EnoceanUtils.getChecksum(rv,2);
    return rv;
  }

  public int getPhysicalAddress() {
    return physicalAddress;
  }
  
  public String getStringAddress() {
    StringBuffer sb = new StringBuffer();
    sb.append( Integer.toHexString( getPhysicalAddress() ) );
    while ( sb.length() < 8 ) {
      sb.insert(0, '0');
    }
    return sb.toString();
  }

  public String getDataPacketAsString() {
    StringBuffer sb = new StringBuffer();
    char[] packet = getDataPacket();
    for (int i = 0; i < packet.length; i++) {
      if (i>0 && (i%2)==0) sb.append(" ");
      sb.append( String.format( "%02x", (int) packet[i] ));
    }
    return sb.toString();
  }

  public int getChannel() {
    return channel ;
  }

}
